package org.springblade.common.config;

import com.iflytek.sc.ssp.uc.client.filter.FilterChainProxy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Objects;

/**
 * 过滤器配置
 **/
@Slf4j
@Configuration
public class UcClientConfig {

	@Value("${uc-config.configUrl}")
	private String configUrl;

	@Value("${uc-config.urlPatterns}")
	private String urlPatterns;

	@Value("${uc-config.environment}")
	private String environment;


	@Bean
	public FilterRegistrationBean<FilterChainProxy> ucClientFilterRegistration() {
		if ("dev".equals(environment)) {
			configUrl = Objects.requireNonNull(this.getClass().getClassLoader().getResource("uc-client.properties")).getPath();
		}
		log.info("统一认证对接配置文件路径-ucClientConfigUrl={}", configUrl);
		FilterRegistrationBean<FilterChainProxy> registration = new FilterRegistrationBean<>();
		registration.setFilter(new FilterChainProxy());
		registration.addUrlPatterns(urlPatterns);
		registration.addInitParameter("uc-config", configUrl);
		registration.addInitParameter("timeOut", "30");
		registration.setName("ucClientFilter");
		registration.setOrder(1);

		return registration;
	}

}
